package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rms.util.Constant;

import Bizlogic.CAManager;
import DAO.DAOException;
import DAO.MySQL.OrderDetailDAOImpl;
import DAO.MySQL.ProductsDAOImpl;
import Model.Product;

/**
 * @author TONY
 *
 */
public class SaveOrderDetails extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String ordernumString=request.getParameter("ordernum");
		String str[]=ordernumString.split(":");	   
		int ordernum=Integer.parseInt(str[1]);
		int partnum=Integer.parseInt(request.getParameter("partnum"));
		int quantity=Integer.parseInt(request.getParameter("quantity"));
		CAManager ca= new CAManager();
		//OrderDetailDAOImpl oddi=new OrderDetailDAOImpl();
		ca.inserOrderDetail(ordernum, partnum, quantity);
		ProductsDAOImpl pdi=new ProductsDAOImpl();
		pdi.updateInstock(partnum, quantity);
		request.getRequestDispatcher("ListParts.do").forward(request, response);
		

	
	}
	
	 
		public void doGet(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			String ua = request.getHeader(Constant.HEAD);
			PrintWriter out = response.getWriter();
			if (Constant.MIDP_USER_AGENT.equals(ua)) {
				
				int ordernum=Integer.parseInt(request.getParameter("ordernum"));
				int partnum=Integer.parseInt(request.getParameter("partnum"));
				int quantity=Integer.parseInt(request.getParameter("quantity"));
				CAManager ca= new CAManager();
				//OrderDetailDAOImpl oddi=new OrderDetailDAOImpl();
				ca.inserOrderDetail(ordernum, partnum, quantity);
				ProductsDAOImpl pdi=new ProductsDAOImpl();
				pdi.updateInstock(partnum, quantity);
				out.print("Usage Added");

			
			

				// EmployeeDAO ed=new EmployeeDAOImpl();

			}
			
			
			
			

		
		}

}
